from pathlib import Path
from datetime import datetime
root_folder = Path('F:\\销售表')
file_list = root_folder.rglob('*.txt')
for f in file_list:
    if f.is_dir():
        continue
    new_folder = root_folder / f.stem
    if not new_folder.exists():
        new_folder.mkdir(parents=True)
    file_time = f.stat().st_mtime
    file_time = datetime.fromtimestamp(file_time)
    file_time = file_time.strftime('%Y年%m月')
    new_file_name =  file_time + '-' + f.name
    new_path = new_folder / new_file_name
    new_path = new_path.with_suffix('.csv')
    f.replace(new_path)
folder_list = root_folder.iterdir()
for f in folder_list:
    if f.is_file():
        continue
    fc = len(list(f.iterdir()))
    if fc == 0:
        f.rmdir()
    